#!/usr/bin/env python3
"""
Ollama Translator - Main Application Entry Point

A desktop translator application that uses locally running Ollama service
as the backend with a Qt-based GUI interface.
"""

import sys
import os

# Set environment variables for proper input method support on Linux
if sys.platform.startswith('linux'):
    # Try different input method modules
    input_methods = ['fcitx', 'ibus', 'xim']
    
    # Set environment variables for input method support
    os.environ['QT_IM_MODULE'] = 'fcitx'  # Try fcitx first
    os.environ['GTK_IM_MODULE'] = 'fcitx'
    os.environ['XMODIFIERS'] = '@im=fcitx'
    
    # Additional Qt settings for better input method support
    os.environ['QT_ENABLE_GLYPH_CACHE_WORKAROUND'] = '1'
    os.environ['QT_QPA_PLATFORM'] = 'xcb'  # Force X11 backend
    os.environ['QT_AUTO_SCREEN_SCALE_FACTOR'] = '0'  # Disable auto scaling
    
    # Enable verbose logging for debugging
    os.environ['QT_LOGGING_RULES'] = 'qt.qpa.inputmethods=true'

# Add the current directory to Python path to ensure imports work correctly
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

try:
    from translator_gui import main as gui_main
except ImportError as e:
    print(f"Error importing required modules: {e}")
    print("Please make sure all dependencies are installed:")
    print("uv sync")
    sys.exit(1)


def main():
    """Main entry point for the Ollama Translator application."""
    print("Starting Ollama Translator...")
    print(f"Platform: {sys.platform}")
    
    if sys.platform.startswith('linux'):
        print("Linux detected - using fcitx input method configuration")
        print(f"QT_IM_MODULE: {os.environ.get('QT_IM_MODULE', 'not set')}")
    
    try:
        gui_main()
    except Exception as e:
        print(f"Fatal error: {e}")
        sys.exit(1)


if __name__ == "__main__":
    main()